const Controller = require('egg').Controller;

class AppController extends Controller {

  async index() {

    const { ctx } = this;

    const { userInfo } = ctx.session;

    await ctx.render('app/app.js', {
      url: ctx.url.replace(/\/app/, ''),
      userInfo,
      device: ctx.device,
      // device: await this.ctx.service.app.initDeviceAndOptions()
    });

  }

  async phoneLogin() {

    const { ctx } = this;

    const url = this.config.serverAddress + '/beeUser/phoneLogin.do';


    const result = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json',
    });

    await ctx.service.app.setUserInfoToSession(result);

    ctx.body = result.data;
    ctx.status = result.status;

  }

  async login() {

    const { ctx } = this;

    const url = this.config.serverAddress + '/beeUser/login.do';

    const result = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json',
    });

    await ctx.service.app.setUserInfoToSession(result);

    ctx.body = result.data;
    ctx.status = result.status;

  }

  async regist() {

    const { ctx } = this;

    const url = this.config.serverAddress + '/beeUser/regist.do';

    const result = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json',
    });

    await ctx.service.app.setUserInfoToSession(result);

    ctx.body = result.data;
    ctx.status = result.status;

  }

  async refreshUserInfo() {
    const { ctx } = this;

    const url = this.config.serverAddress + '/beeUser/selectBeeUser.do';

    const result = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json',
    });

    await ctx.service.app.setUserInfoToSession(result);

    ctx.body = result.data;
    ctx.status = result.status;

  }

  async logout() {
    const { ctx } = this;

    const url = this.config.serverAddress + '/beeUser/logout.do';

    const result = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json',
    });

    ctx.session.userInfo = null;

    ctx.body = result.data;
    ctx.status = result.status;
  }

  async baiduApi() {
    const { ctx } = this;
    const url = ctx.method === 'GET' ? ('http://api.map.baidu.com' + ctx.path.split('baiduApi')[1] + '?' + ctx.querystring) : ('http://api.map.baidu.com' + ctx.path.split('baiduApi')[1]);
    const res = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'text'
    });

    const str = res.data.substring(res.data.indexOf('{"status"'), res.data.length - 1);
    ctx.body = str;
    ctx.status = res.status;
  }

  async proxy() {

    const { ctx } = this;

    const url = ctx.method === 'GET' ? (this.config.serverAddress + ctx.path + '?' + ctx.querystring) : (this.config.serverAddress + ctx.path);

    const res = await ctx.curl(url, {
      method: ctx.method,
      data: ctx.request.body,
      dataType: 'json'
    });

    ctx.body = res.data;
    ctx.status = res.status;

  }

}

module.exports = AppController;
